﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Wp.DAL;
using Tools;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for ActionsRolesBase
    /// </summary>
    public class TblAlbumManagerBase
    {
        public TblAlbumManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public int InsertUpdate(TblAlbumEntity item)
        {
            List<SqlParameter> list = new List<SqlParameter>();
            SqlParameter parameter = new SqlParameter("@Output", item.Id)
            {
                Direction = ParameterDirection.Output
            };
            list.Add(parameter);
            list.Add(new SqlParameter("@Id", item.Id));
            list.Add(new SqlParameter("@TextId", item.TextId));
            list.Add(new SqlParameter("@Name", item.Name));
            list.Add(new SqlParameter("@ImageUrl", item.ImageUrl));
            list.Add(new SqlParameter("@DisplayOrder", item.DisplayOrder));
            list.Add(new SqlParameter("@IsVisible", item.IsVisible));
            list.Add(new SqlParameter("@CreatedBy", item.CreatedBy));
            list.Add(new SqlParameter("@CreatedDate", item.CreatedDate));
            list.Add(new SqlParameter("@ModifiedBy", item.ModifiedBy));
            list.Add(new SqlParameter("@ModifiedDate", item.ModifiedDate));
            SqlParameter[] commandParameters = list.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblAlbum", commandParameters);
            return int.Parse(commandParameters[0].Value.ToString());
        }

        public int InsertUpdate(int Id, string TextId, string Name, string ImageUrl, int DisplayOrder, bool IsVisible, string CreatedBy, DateTime CreatedDate, string ModifiedBy, DateTime ModifiedDate)
        {
            List<SqlParameter> list = new List<SqlParameter>();
            SqlParameter item = new SqlParameter("@Output", Id)
            {
                Direction = ParameterDirection.Output
            };
            list.Add(item);
            list.Add(new SqlParameter("@Id", Id));
            list.Add(new SqlParameter("@TextId", TextId));
            list.Add(new SqlParameter("@Name", Name));
            list.Add(new SqlParameter("@ImageUrl", ImageUrl));
            list.Add(new SqlParameter("@DisplayOrder", DisplayOrder));
            list.Add(new SqlParameter("@IsVisible", IsVisible));
            list.Add(new SqlParameter("@CreatedBy", CreatedBy));
            list.Add(new SqlParameter("@CreatedDate", CreatedDate));
            list.Add(new SqlParameter("@ModifiedBy", ModifiedBy));
            list.Add(new SqlParameter("@ModifiedDate", ModifiedDate));
            SqlParameter[] commandParameters = list.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblAlbum", commandParameters);
            return int.Parse(commandParameters[0].Value.ToString());
        }
        public string GetUniqueTextIdFromUnicodeText(string UnicodeText)
        {
            string strSql = @"  SELECT TextId FROM TblAlbum tn
                                WHERE tn.TextId=@TextId";
            string textId = WebUtility.GetUniqueTextIdFromUnicodeText(UnicodeText, "@TextId", strSql
                , SqlHelper.WpConnectString, 255);
            return textId;
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_DeleteTblAlbum", sqlparam);
        }
        public TblAlbumEntity GetTblAlbumEntity(DataRow dr)
        {
            TblAlbumEntity item = new TblAlbumEntity();            
            item.Id = dr["Id"].ToInt();              
            item.TextId = dr["TextId"].ToString();              
            item.Name = dr["Name"].ToString();              
            item.ImageUrl = dr["ImageUrl"].ToString();              
            item.DisplayOrder = dr["DisplayOrder"].ToInt();              
            item.IsVisible = dr["IsVisible"].ToBoolean();              
            item.CreatedBy = dr["CreatedBy"].ToString();              
            item.CreatedDate = dr["CreatedDate"].ToDateTime();              
            item.ModifiedBy = dr["ModifiedBy"].ToString();              
            item.ModifiedDate = dr["ModifiedDate"].ToDateTime();              
            return item;
        }
        
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblAlbum", sqlparam);
        }
        public TblAlbumEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblAlbum", sqlparam);
            if(dt!=null && dt.Rows.Count>0)
                return GetTblAlbumEntity(dt.Rows[0]);
            return null;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblAlbumsAll", null);
        }
        public List<TblAlbumEntity> SelectAll()
        {
            List<TblAlbumEntity> lsresult= new List<TblAlbumEntity>();
            DataTable dt =  SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblAlbumsAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblAlbumEntity(dt.Rows[i]));            
            return lsresult;
        }
    }
}

